#include<bits/stdc++.h>
using namespace std;


long long ans;

int cnt[1000010],a[200010];

int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T;
    cin>>T;
    while(T--){
        queue <int> q;
        memset(cnt,0,sizeof cnt);
        int n;
        ans = 0;
        cin>>n;
        for(int i=1;i<=n;i++){
            scanf("%d",a+i);
            cnt[a[i]]++;
        }
        int tot = 0;
        for(int i=1;i<=n;i++){
            if(cnt[a[i]]==1) continue;
            else{
                if(q.front()==a[i]){
                    cnt[a[i]]-=2;
                    ans += a[i];
                    q.pop();
                }
                else q.push(a[i]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}